package cm.tl.pms.common.enums;

import cm.tl.pms.common.exception.CustomErrorException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Getter
public enum DocFormat {
    JPG("jpg", "1"),
    PNG("png", "2"),
    GIF("gif", "3"),
    MP4("mp4", "4"),
    MOV("mov", "5"),
    MP3("mp3", "6"),
    TXT("txt", "7"),
    PDF("pdf", "8"),
    DOC("doc", "9"),
    DOCX("docx", "10");

    private final String suffix;
    private final String number;

    private static final Map<String, DocFormat> suffixToEnum = new HashMap<>();
    private static final Map<String, DocFormat> numberToEnum = new HashMap<>();

    static {
        for (DocFormat format : values()) {
            suffixToEnum.put(format.getSuffix(), format);
            numberToEnum.put(format.getNumber(), format);
        }
    }

    public static String fromSuffix(String suffix) {
        try {
            return suffixToEnum.get(suffix).getNumber();
        } catch (Exception e) {
            throw new CustomErrorException("不支持的文件格式");
        }
    }

    public static String fromNumber(String number) {
        return numberToEnum.get(number).getSuffix();
    }


    public static void main(String[] args) {
        System.out.println(fromSuffix("png"));
    }
}
    